package mysql

import (
	"database/sql"
	"errors"
	"news/constStatus"
	"news/models"

	"go.uber.org/zap"
)

var ErrorCommunityNameExist = errors.New("社区分类名称已经存在")
var ErrorCommunityInvalidID = errors.New("无效的id")
var ErrorCommunityQueryFailed = errors.New("查询失败")

// 判断社区是否有重复
func CheckCommunityNameExit(communityName string) (err error) {
	var count int
	sqlStr := "select count(community_id) from community where community_name = ? "
	err = db.Get(&count, sqlStr, communityName)
	if err != nil {
		return constStatus.ErrorNOTTable
	}
	if count > 0 {
		return ErrorCommunityNameExist
	}
	return
}

// 保存社区信息写入数据库
func InsertCommunity(community *models.Community) (err error) {
	if community.Id > 0 {
		sqlStr := "update community" +
			" set community_id= ? ,community_name= ?,introduction= ? where id = ?"
		ret, err := db.Exec(sqlStr, community.CommunityId, community.CommunityName, community.Introduction, community.Id)
		affected, _ := ret.RowsAffected()
		if err != nil || affected < 1 {
			zap.L().Error("update failed", zap.String("CommunityId", string(community.Id)), zap.Error(err))
			return ErrorCommunityQueryFailed
		}
	} else {
		sqlStr := "insert into" +
			" community(community_id ,community_name,introduction)" +
			" values(?,?,?)"
		_, err = db.Exec(sqlStr, community.CommunityId, community.CommunityName, community.Introduction)
		zap.L().Error("community inset data failed", zap.Error(err))
		return err
	}
	return
}

// 编辑社区信息入库
func CommunityEdit(CommunityID int64) (communityCatList *models.Community, err error) {
	communityCatList = new(models.Community)
	sqlStr := "select " +
		"id ,community_id ,community_name,introduction " +
		"from community where id = ?"
	err = db.Get(communityCatList, sqlStr, CommunityID)
	if err == sql.ErrNoRows {
		err = ErrorCommunityInvalidID
		return
	}
	if err != nil {
		zap.L().Error("query community failed", zap.String("sql", sqlStr),
			zap.Error(err))
		err = ErrorCommunityQueryFailed
		return
	}
	return
}

//社区列表查询
func GetCommunityList() (communityList []*models.Community, err error) {
	sqlStr := "select id ,community_id ,community_name,introduction from community"
	err = db.Select(&communityList, sqlStr)
	return
}

//社区详情id数据
func GetCommunityDetailByID(id int64) (community *models.Community, err error) {
	community = new(models.Community)
	sqlStr := "select " +
		"id ,community_id ,community_name,introduction " +
		"from community " +
		"where id = ? "
	err = db.Get(community, sqlStr, id)
	if err == sql.ErrNoRows {
		return community, err
	}
	if err != nil {
		zap.L().Error("query community failed", zap.String("sql", sqlStr),
			zap.Error(err))
		return community, ErrorCommunityQueryFailed
	}
	return community, err
}

// 根据post 表的 社区ID 查询 分类名称
func GetCommunityByPostID(idStr int64) (community *models.Community, err error) {
	community = new(models.Community)
	sqlStr := `select 
				community_id, community_name 
				from community 
				where community_id = ?`
	err = db.Get(community, sqlStr, idStr)
	if err == sql.ErrNoRows {
		err = ErrorCommunityInvalidID
		return
	}
	if err != nil {
		zap.L().Error("query community failed", zap.String("sql", sqlStr),
			zap.Error(err))
		err = ErrorCommunityQueryFailed
		return
	}
	return
}
